package com.laoyao.service;



import java.util.ArrayList;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.aop.Duang;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.laoyao.beans.JsCode;
import com.laoyao.comm.model.User;
import com.laoyao.util.DateUtil;
import com.laoyao.util.HttpUtil;
import com.laoyao.util.MD5Util;
import com.laoyao.util.TextUtil;
import com.laoyao.util.UIDUtil;

public class UserService {
	public static final UserService me = Duang.duang(UserService.class);
	private static UserService dao = Duang.duang(UserService.class);
	/**
	 * 用户账号查找用户
	 */
	public User findUserByAccount(String account){
		String sql = "select * from v_user where account = ?";
		return User.dao.findFirst(sql,account);
	}
	/**
	 * 手机号查找用户
	 */
	public User findUserByTel(String tel){
		String sql = "select * from v_user where tel = ?";
		return User.dao.findFirst(sql,tel);
	}
	/**
	 * 通过账号和密码进行登录
	 * @param account
	 * @param pwd
	 * @return
	 */
	public User userLogin(String account,String pwd){
		String sql = "select id,account,userName,photo,sex,birthday,state,level,currentExp,upgradeExp,signature,registerTime,changeNameNum,role,vipTime"
				+ " from v_user where account=? and pwd =?";
		System.out.println(pwd);
		return User.dao.findFirst(sql,account,pwd);
	}
	
	/**
	 * 通过id查找用户
	 * @param userId
	 * @return
	 */
	public User findUserById(Integer userId){
		String sql = "select id,account,userName,photo,sex,birthday,state,level,currentExp,upgradeExp,signature,registerTime,changeNameNum"
				+ " from v_user where id=?";
		return User.dao.findFirst(sql,userId);
	}
	
	/**
	 * 获取粉丝数量
	 * @param id
	 * @return
	 */
	public Long getFansNum(String id){
		String sql = "select count(*) num from v_fans where rid=?";
		return Db.findFirst(sql,id).getLong("num");
	}
	/**
	 * 获取我关注的人的数量
	 * @param id
	 * @return
	 */
	public Long getLikeNum(String id){
		String sql = "select count(*) num from v_fans where uid=?";
		return Db.findFirst(sql,id).getLong("num");
	}
	/**
	 * 是否关注
	 * @return
	 */
	public boolean isFans(String uid,String rid){
		String sql ="select * from v_fans where uid=? and rid=?";
		return Db.findFirst(sql,uid,rid)!=null;
	}
	/**
	 * 返回用户列表
	 * @param pageNumber
	 * @param pageSize
	 * @param userName
	 * @return
	 */
	
	public Page<Record> userList(Integer pageNumber,Integer pageSize,String userName){
		String sql  = "select * from v_user where 1=1 ";
		List<String> list = new ArrayList();
		if(!TextUtil.isEmpty(userName)){
			sql+=" and userName like ? ";
			list.add(userName+"%");			
		}
		sql+=" order by id desc";
		return Db.paginate(pageNumber, pageSize, sql,list.toArray());
	}
	
	public boolean setState(String id,String state){
		User u = new User();
		u.set("state", state).set("id", id);
		return u.update();
	}
	/**
	 * 检验验证码是否有效
	 * @param token
	 * @param code
	 * @return
	 */
	public boolean checkCode(String token,String code){
		String sql = "select * from v_code where token=? and code=?";
		Record r = Db.findFirst(sql,token,code);
		return r!=null&&DateUtil.dateCmp(r.getStr("outTime"),DateUtil.getTime() )==1;
	}


}
